/*
 * MIT License
 * 
 * Copyright (c) 2017 wen.gu <454727014@qq.com>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: tiny_ini_file.h
 *
 * Purpose: ini file operation API implementation
 *
 * Developer:
 *   wen.gu , 2016-08-10
 *
 * TODO: 
 *
 ***************************************************************************/

#ifndef __TINY_INI_FILE_H__
#define __TINY_INI_FILE_H__


#include "general_type.h"

#ifdef __cplusplus
extern "C"{
#endif

/**
 * ini file 
 * syntax:
 * []     <   section lable,eg. [log.conf]
 * #      <   one line comment
 * ;      <   one line comment
 * =      <   link key(at left) and value(at right),eg. level = debug
 *
 * @note  1. the key in a section is not unique, it mean that, maybe have
 *           several same key in one section.
 *
 * for example:
 *
 * [log.conf] #log print configure
 * level = debug
 * enable_line_number = true
 *
 * ;pluing list
 * [plugin.list]
 * plugin = libfdk_aac_enc.so
 * plugin = libx264_enc.so
 *
 */


/***************************************************************************
 *
 * macro declaration
 *
 ***************************************************************************/
/**
* if ini file havan't any section,then parser will add a default section
* user cann't use this section name,it is reserve section name.
*/

#define DEFAULT_SECTION_NAME "default"

#define SECTION_MAX_COUNT  32  /** maximu section count in one ini file */

#define LINE_BUF_MAX_LEN   1024 /** the expression line maximum length */

/***************************************************************************
 *
 * data structure declaration
 *
 ***************************************************************************/
typedef struct key_value_pair_s key_value_pair_t;

typedef struct ini_section_s 
{
    int pair_count; /**the conut of pairs in current section */   
    key_value_pair_t* pairs;
    char *name; /** section name */
    void* opaque; /** private data, user cann't modify it */
}ini_section_t;

/***************************************************************************
 *
 * API declaration
 *
 ***************************************************************************/
/*
 *@brief create a ini file instance
 *
 *@param none.
 *
 *@return sucess: ini file instance handle, fail: NULL.
 *
 **/
 G_API GPHD tfIniFileCreate(void);

/*
 *@brief release ini parser
 *
 *@param ini_file  [in]  ini file instance pointer
 *
 *@return none.
 *
 *@note
 *
 **/
 G_API void tfIniFileDestroy(GPHD ini_file);

/*
 *@brief load a ini file
 *
 *@param ini_file  [in] ini file instance pointer
 *@param file_name [in] the name of ini file
 *
 *@return sucess: G_OK, fail: error code.
 *
 **/
 G_API GS32 tfIniFileLoad(GPHD ini_file, const char* file_name);

/*
 *@brief get the value of a key which is first key in the section.
 *
 *@param ini_file [in]  ini file instance pointer
 *@param section  [in]  the name of section
 *@param key      [in]  the name of key
 *@param value    [out] the value of the key
 *
 *@return sucess: G_OK, fail: error code.
 *
 *@note maybe have many same key in one section, here just get we find
 *      the first value with "key"
 *
 **/
 G_API GS32 tfIniFileGetValue(GPHD ini_file, const char* section, const char* key, char** value);

/*
*@brief get all values of a key in the section.
*
*@param ini_file [in]  ini file instance pointer
*@param section  [in]  the name of section
*@param key      [in]  the name of key
*@param values   [out] the all values of the key
*@param count    [out] the count of values
*
*@return sucess: G_OK, fail: error code.
*
*@note 
*
**/
 G_API GS32 tfIniFileGetValues(GPHD ini_file, const char* section, const char* key, char** values[], GU32* count);


/*
*@brief free values
*
*@param ini_file [in] ini file instance pointer
*@param values   [in] the values which need to free
*@param count    [in] the count of values 
*
*@return none.
*
*@note 
*
**/
 G_API void tfIniFileFreeValues(GPHD ini_file, char* values[], GU32 count);

/*
*@brief get a section with section name.
*
*@param ini_file [in]  ini file instance pointer
*@param name     [in]  the name of section
*@param key      [in]  the name of key
*@param section  [out] required section pointer
*
*@return sucess: G_OK, fail: error code.
*
*@note 
*
**/
G_API GS32 tfIniFileGetSection(GPHD ini_file, const char* name, ini_section_t** section);

/*
*@brief detect if there is a key in the current section
*
*@param ini_file [in] ini file instance pointer
*@param section  [in] the name of section
*@param key      [in] the name of key
*
*@return have: GTRUE, haven't: GFALSE
*
*@note 
*
**/
G_API GBOL tfIniFileHasKey(GPHD ini_file, const char* section, const char* key);

/*
*@brief detect if there is a section
*
*@param ini_file [in] ini file instance pointer
*@param section  [in] the name of section
*
*@return have: GTRUE, haven't: GFALSE
*
*@note 
*
**/
G_API GBOL tfIniFileHasSection(GPHD ini_file, const char* section);

/*
*@brief print all data in ini file
*
*@param ini_file [in] ini file instance pointer
*
*@return none
*
*@note 
*
**/
G_API void tfIniFileDump(GPHD ini_file);

#ifdef __cplusplus
}
#endif


#endif //end of __TINY_INI_FILE_H__
